import { IMenu } from "../types/login";
/**
 * 将日期对象转换为“年-月-日 时:分:秒”格式的字符串
 * @param date - 要转换的日期对象
 * @returns 格式化后的日期字符串
 */
export function formatDateTime(date: Date): string {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export function formateState(state: number) {
  switch (state) {
    case 1:
      return "在职";
    case 2:
      return "试用期";
    case 3:
      return "离职";
    default:
      return "离职";
  }

  //   if (state === 1) {
  //     return "在职";
  //   } else if (state === 2) {
  //     return "试用期";
  //   } else {
  //     return "离职";
  //   }
}

// 递归获取路由对象
export const searchRoute: any = (path: string, routes: any[]) => {
  for (const item of routes) {
    if (item.path === path) return item;
    if (item.children) {
      const res = searchRoute(path, item.children);
      if (res) return res;
    }
  }
};

export function getMenuPath(list: IMenu[]): string[] {
  return list.reduce((res: string[], item: IMenu) => {
    return res.concat(
      Array.isArray(item.children) && !item.buttons
        ? getMenuPath(item.children)
        : item.path + ""
    );
  }, []);
}

export const findTreeNode = (treeData: IMenu[], pathName: string, path: string[]): string[] => {
  if (!treeData) return [];
  for (const item of treeData) {
      path.push(item.menuName);
      if (item.path === pathName) return path;
      if (item.children?.length) {
          const list = findTreeNode(item.children, pathName, path);
          if (list.length) return list;
      }
      path.pop();
  }
  return [];
};
